﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AspNetExpressInformationManageSystem.Models;

namespace AspNetExpressInformationManageSystem.Controllers
{
    [Authorize(Roles = "Receiver,Distributor")]
    public class ExpressGoodsController : Controller
    {
        private ExpressDbContext db = new ExpressDbContext();

        // GET: ExpressGoods
        public ActionResult Index()
        {
            return View(db.ExpressGoods.ToList());
        }

        // GET: ExpressGoods/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoods expressGoods = db.ExpressGoods.Find(id);
            if (expressGoods == null)
            {
                return HttpNotFound();
            }
            return View(expressGoods);
        }

        // GET: ExpressGoods/Create
        public ActionResult Create()
        {
            return View(new ExpressGoods() { Status = "已收件", Date = DateTime.Now.Date });
        }

        // POST: ExpressGoods/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,SenderName,SenderTel,SenderAddr,SenderZipCode,ReceiverName,ReceiverTel,ReceiverAddr,ReceiverZipCode,Weight,Size,Cost,Remark,Status,Date")] ExpressGoods expressGoods)
        {
            if (ModelState.IsValid)
            {
                db.ExpressGoods.Add(expressGoods);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(expressGoods);
        }

        // GET: ExpressGoods/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoods expressGoods = db.ExpressGoods.Find(id);
            if (expressGoods == null)
            {
                return HttpNotFound();
            }
            return View(expressGoods);
        }

        // POST: ExpressGoods/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,SenderName,SenderTel,SenderAddr,SenderZipCode,ReceiverName,ReceiverTel,ReceiverAddr,ReceiverZipCode,Weight,Size,Cost,Remark,Status,Date")] ExpressGoods expressGoods)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expressGoods).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(expressGoods);
        }

        // GET: ExpressGoods/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoods expressGoods = db.ExpressGoods.Find(id);
            if (expressGoods == null)
            {
                return HttpNotFound();
            }
            return View(expressGoods);
        }

        // POST: ExpressGoods/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ExpressGoods expressGoods = db.ExpressGoods.Find(id);
            db.ExpressGoods.Remove(expressGoods);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Search()
        {
            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Search([Bind(Include = "ID,Date,ZipCode")] GoodsSearch goodsSearch)
        {
            if (goodsSearch.ID != 0)
            {
                ExpressGoods goods = db.ExpressGoods.Find(goodsSearch.ID);
                if (goods != null)
                {
                    return View("Details", goods);
                }
            }
            var result = db.ExpressGoods.AsQueryable();

            if (!goodsSearch.Date.Equals(DateTime.MinValue))
            {
                result = result.Where(s => s.Date.Equals(goodsSearch.Date));
            }

            if (!String.IsNullOrEmpty(goodsSearch.ZipCode))
            {
                result = result.Where(s => s.SenderZipCode == goodsSearch.ZipCode || s.ReceiverZipCode == goodsSearch.ZipCode);
            }
            return View("Index", result.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
